<template>
  <div v-loading="loading" >

    <!-- <label>省份</label> -->
    <el-form-item label="省份" prop="province">
      <el-select v-model="form.province" :disabled="isdisabledFn" placeholder="请选择" class="search-input" @change="changeProvince">
        <el-option
          v-for="item in provinceOptions"
          :key="item.region_id"
          :label="item.region_name"
          :value="item.region_id"/>
      </el-select>
    </el-form-item>
    <!-- <label>城市</label> -->
    <el-form-item label="城市" prop="city">
      <el-select v-model="form.city" :disabled="isdisabledFn" placeholder="请选择" class="search-input" @change="changeCity">
        <el-option
          v-for="item in cityOptions"
          :key="item.region_id"
          :label="item.region_name"
          :value="item.region_id"/>
      </el-select>
    </el-form-item>
    <!-- <label>区县</label> -->
    <!-- <el-form-item label="区县" prop="county">
        <el-select v-model="form.county" placeholder="请选择" class='form-input'>
            <el-option v-for="item in countyOptions" :key="item.region_id"
            :label="item.region_name" :value="item.region_id"> </el-option>
        </el-select>
    </el-form-item> -->
  </div>
</template>

<script>
import { getDealer } from '@/api/admin/org/dealerInfo'
import { mapGetters } from 'vuex'
import _ from 'lodash'
export default {
  name: 'ProvinceCity',

  props: {},
  data() {
    return {
      model: ['1', '2', '3'],
      form: { province: '', city: '', county: '' },
      rules: {
        province: [{ required: true, message: '请选择省份', trigger: 'blur' }],
        city: [{ required: true, message: '请选择城市', trigger: 'blur' }],
        county: [{ required: true, message: '请选择区县', trigger: 'blur' }]
      },
      provinceOptions: [],
      cityOptions: [],
      countyOptions: [],
      isdisabledFn: false

    }
  },
  computed: {
    ...mapGetters(['dictMap', 'regionMap'])
  },
  created() {
    this.loadData()
  },
  mounted() {
    this.loadData()
    // log('dictMap:',this.dictMap);
    const array = Object.values(this.regionMap)
    this.provinceOptions = array
  },

  methods: {
    loadData() {
      this.loading = true
      getDealer().then(res => {
        this.loading = false
        res.province = Number(res.province)
        res.city = Number(res.city)
        res.county = Number(res.county)
        this.form = _.cloneDeep(res)
        this.setOptions()
      })
        .catch(err => {
          this.loading = false
          // table.complete()
        })
    },
    changeProvince() {
      const p = this.form.province
      this.cityOptions = this.regionMap[p].children
      this.form.city = ''
      this.form.county = ''
    },
    changeCity() {
      const p = this.form.province
      const c = this.form.city
      this.form.county = ''
      this.countyOptions = this.regionMap[p].children[c].children
    },
    setOptions() {
      const p = this.form.province
      this.cityOptions = this.regionMap[p].children
      const c = this.form.city
      this.countyOptions = this.regionMap[p].children[c].children
    }
  }
}
</script>

<style scoped>

</style>
